<template>
  <div class="app-container">
    <div class="top">
      <div class="operate">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workMgmt:workSection:add']"
          >新增
        </el-button>
        <el-button
          icon="el-icon-edit"
          size="mini"
          :disabled="single != true"
          @click="handleUpdate()"
          v-hasPermi="['workMgmt:workSection:edit']"
          >编辑
        </el-button>
        <el-button
          icon="el-icon-delete"
          size="mini"
          :disabled="single != true && single != false"
          @click="handleDelete()"
          v-hasPermi="['workMgmt:workSection:remove']"
          >删除
        </el-button>
      </div>
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="请输入关键字"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="Table"
      :data="list"
      tooltip-effect="dark"
      style="width: 100%"
      height="calc(100% - 44px - 50px)"
      v-loading="loading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" type="index" width="80" align="center">
        <template slot-scope="scope">
          <span>{{
            utils.tableSerialNumber(scope, pager.pageNum, pager.pageSize)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工段编码" prop="workSectionCode">
        <template slot-scope="{row}">
          <el-button type="text" @click="handleUpdate(row)">{{row.workSectionCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="工段名称" prop="workSectionName"> </el-table-column>
      <el-table-column label="工序描述" prop="remark"> </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="{row}">
          <el-button v-hasPermi="['workMgmt:workSection:edit']" type="text" @click.stop="handleUpdate(row)">编辑</el-button>
          <el-button v-hasPermi="['workMgmt:workSection:remove']" type="text" @click.stop="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="pager.total"
      :page.sync="pager.pageNum"
      :limit.sync="pager.pageSize"
      @pagination="getList"
    />
    <DetailDialog :visible.sync="detailDialog.visible" :row="detailDialog.row" @submit-success="getList"></DetailDialog>
  </div>
</template>
<script>
import {
  listWorkSection,
  delWorkSection } from "@/api/workMgmt/workSection";
import DetailDialog from "./detailDialog";
import organizationCascader from "@/views/components/organizationCascader";
export default {
  name: "WorkSection",
  components: { DetailDialog, organizationCascader },
  data() {
    return {
      queryParams: {
        searchValue: "",
      },
      list: [],
      single: null,
      loading: false,
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      detailDialog: {
        visible: false,
        row: null
      }
    };
  },
  created() {
    //获取列表数据
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      this.single = null;
      const { pageNum, pageSize } = this.pager
      const params = {
        query: {
          ...this.queryParams,
        },
        pageNum,
        pageSize
      }
      listWorkSection(params)
      .then(response => {
        if (response.code == 200) {
          this.list = response.rows || [];
          this.pager.total = response.total;
        } else {
          this.list = []
          this.pager.total = 0
        }
        this.loading = false;
      }).catch(() => {
        this.list = []
        this.pager.total = 0
        this.loading = false;
      });
    },
    handleSelectionChange(selection) {
      if (selection.length > 1) {
        this.single = false;
      } else if (selection.length == 1) {
        this.single = true;
      } else {
        this.single = null;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.detailDialog.row = null
      this.detailDialog.visible = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (!row) {
        row = this.$refs.Table.selection[0] || null
      }
      this.detailDialog.row = row
      this.detailDialog.visible = true
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let ids = ''
      let msg = ''
      if (row) {
        ids = row.id
        msg = `确定删除${row.workSectionName}吗？`
      } else {
        const selection = this.$refs.Table.selection
        ids = selection.map(ele => ele.id).join(',')
        msg = `确定删除所选的${selection.length}条记录吗？`
      }
      this.$confirm(msg, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
      }).then(function () {
          return delWorkSection(ids);
      }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
      })
    },
    /**
    handleExport() {

    }
    **/
  },
};
</script>
<style lang="scss" scoped>
.top {
  height: 44px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.pagination-container {
  margin: 0;
}
</style>
